#!/bin/bash

if [ $(whoami) != "root" ];then
	echo "请使用root权限执行Java环境配置命令！"
	exit 1;
fi

echo -e "\033[1;32mWelcome to InstallJava\033[0m"

read -p "java路径：" java
echo -e "----------------------------------------------------"
echo "当前输入的java路径：${java}"
read -p "输入yes安装: " yes;
if [ "$yes" != "yes" ];then
	echo -e "------------"
	echo "取消安装"
	exit;
fi
echo -e "----------------------------------------------------"
echo "删除旧硬链接"
rm -f /usr/bin/jdk
echo -e "----------------------------------------------------"
echo "生成硬链接"
ln -s ${java} /usr/bin/
echo -e "----------------------------------------------------"
echo "删除旧Java环境配置"
sed -i '/JAVA_HOME/d' /etc/profile

echo -e "----------------------------------------------------"
echo "配置Java环境"
cat >>/etc/profile <<'EOF'
export JAVA_HOME=/usr/bin/jdk
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
EOF
echo -e "----------------------------------------------------"
echo "生效配置的环境"
source /etc/profile

echo -e "----------------------------------------------------"
echo "完成java环境配置，请重启"
echo -e "----------------------------------------------------"
read -p "输入yes重启: " yes;
if [ "$yes" == "yes" ];then
	echo -e "------------"
	reboot
	exit;
fi